const goodsModel = require("../model/goodsModel.js")
class goods {
	static async getGoodsList(ctx) {
		let {
			nowPage,
			pageSize
		} = ctx.request.query
		if (!nowPage) {
			nowPage = 1
		}
		if (!pageSize) {
			pageSize = 10
		}
		let res = await goodsModel.getGoodsList(nowPage, pageSize)
		ctx.body = {
			code: 0,
			codeInfo: "成功",
			data: res
		}
	}
	// 添加商品
	static async addGoods(ctx) {
		// 从post中 把这些取出来
		let {
			img,
			price,
			name,
			concats,
			tell,
			detail,
			guize,
			tuigai,
			address,
			lng,
			lat,
			status,
			cid,
			tag,
			sku,
			position
		} = ctx.request.body
		let arr = {
			img,
			price,
			name,
			concats,
			tell,
			detail,
			address,
			lng,
			lat,
			status,
			cid,
			position
		}
		// 判断是否有参数 为空
		for (let item in arr) {
			if (!arr[item]) {
				ctx.body = {
					code: 1,
					codeInfo: item + "参数不足！"
				}
				break
				return
			}
		}

		if (tag.length === 0) {
			ctx.body = {
				code: 2,
				codeInfo: "tag不能为空！"
			}
			return
		}

		if (sku.length === 0) {
			ctx.body = {
				code: 3,
				codeInfo: "sku不能为空！"
			}
			return
		}

		// 添加商品
		let goodsRes = await goodsModel.addGoods(img, price, name, concats, tell, detail, guize, tuigai, address,
			lng, lat, status, cid,position)

		if (goodsRes.insertId) {
			for (let i = 0; i < tag.length; i++) {
				let {
					name,
					color
				} = tag[i]
				await goodsModel.addTag(name, goodsRes.insertId, color)
			}

			for (let i = 0; i < sku.length; i++) {
				let {
					name,
					miaoshu,
					price,
					num,
					kaishi,
					jieshu
				} = sku[i]
				await goodsModel.addSku(goodsRes.insertId, name, miaoshu, price, num, kaishi, jieshu)
			}
		}
		ctx.body = {
			code: 0,
			codeInfo: "添加成功",
			data: goodsRes
		}
	}

	// 修改商品
	static async editGoods(ctx) {
		// 从post中 把这些取出来
		let {
			id,
			img,
			price,
			name,
			concats,
			tell,
			detail,
			guize,
			tuigai,
			address,
			lng,
			lat,
			status,
			cid,
			tag,
			sku,
			position
		} = ctx.request.body
		let arr = {
			id,
			img,
			price,
			name,
			concats,
			tell,
			detail,
			address,
			lng,
			lat,
			status,
			cid,
			position
		}
		// 判断是否有参数 为空
		for (let item in arr) {
			if (!arr[item]) {
				ctx.body = {
					code: 1,
					codeInfo: item + "参数不足！"
				}
				break
				return
			}
		}

		if (tag.length === 0) {
			ctx.body = {
				code: 2,
				codeInfo: "tag不能为空！"
			}
			return
		}

		if (sku.length === 0) {
			ctx.body = {
				code: 3,
				codeInfo: "sku不能为空！"
			}
			return
		}

		// 修改商品
		await goodsModel.editGoods(id, img, price, name, concats, tell, detail, guize, tuigai, address, lng, lat,
			status, cid,position)


		for (let i = 0; i < tag.length; i++) {
			let {
				name,
				color
			} = tag[i]
			if (tag[i].id) {
				await goodsModel.editTag(tag[i].id, name, id, color)
			} else {
				await goodsModel.addTag(name, id, color)
			}

		}

		for (let i = 0; i < sku.length; i++) {
			let {
				name,
				miaoshu,
				price,
				num,
				kaishi,
				jieshu
			} = sku[i]
			if (sku[i].id) {
				await goodsModel.editSku(sku[i].id, id, name, miaoshu, price, num, kaishi, jieshu)
			} else {
				await goodsModel.addSku(id, name, miaoshu, price, num, kaishi, jieshu)
			}

		}

		ctx.body = {
			code: 0,
			codeInfo: "修改成功",
		}
	}

	static async delGoods(ctx) {
		let {
			id
		} = ctx.request.query
		if (id) {
			await goodsModel.delGoods(id)
		} else {
			ctx.body = {
				code: 1,
				codeInfo: "参数不足"
			}
			return
		}
		ctx.body = {
			code: 0,
			codeInfo: "成功"
		}

	}
}
module.exports = goods